/*
 * @Author: liuxiao
 * @Date: 2023-11-27 00:33:01
 * @Last Modified by: liuxiao
 * @Last Modified time: 2023-12-09 16:46:27
 * @ps: 路由层，转发匹配的路由交给控制器处理，可以处理多个中间件
 *
 */

const Router = require('koa-router')

//导入中间件
const {
  userValidator,
  verifyUser,
  crpytPassword,
  verifyLogin
} = require('../middleware/user.middleware')

const { auth } = require('../middleware/auth.middleware')

//接口赋值，register
const {
  register,
  login,
  changePassword,
  loginTest,
  test
} = require('../controller/user.controller')

const router = new Router({ prefix: '/users' })

//注册接口,对应控制器中的方法
router.post('/register', userValidator, verifyUser, crpytPassword, register)

//登录接口,对应控制器中的方法
router.post('/login', verifyLogin, login)

//测试登录接口
router.post('/loginTest', loginTest)

//修改密码接口
router.patch('/', auth, changePassword, (ctx, next) => {
  console.log(ctx.state.user)
  ctx.body = '修改密码成功！'
})

//GET请求
router.get('/', (ctx, next) => {
  ctx.body = 'hello users'
})

module.exports = router
